<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级" prop="classId">
          <el-select v-model="queryParams.classId" clearable placeholder="请选择班级" filterable >
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="学生姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入获奖名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="奖项类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择奖项类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="奖项级别" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择奖项级别" clearable size="small">
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="颁奖单位" prop="depart">
        <el-input
          v-model="queryParams.depart"
          placeholder="请输入颁奖单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖时间" prop="getTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="info" icon="el-icon-download" size="mini" @click="handleExport" >导出</el-button>
      </el-form-item>
    </el-form>
    <div class="chart-wrapper">
      <table-chart v-if="chart.dsId" :chartData="chart" :params="query" :pager="true"/>
    </div>
  </div>
</template>

<script>
import TableChart from '@/components/Charts/TableChart.vue'
import { getChartByCode} from "@/api/sys/chart";
import { selectClazz } from "@/api/base/clazz";
import { listStudent as selectStudent} from "@/api/base/student";
import { exportDataset} from "@/api/sys/dataset";

export default {
  name: "StudentGloryChart",
  components: {TableChart},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 选中数组
      chartCode: 'student_glory_query',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 查询参数
      queryParams: {
        title: null,
        name: null,
        classId: null,
        type: null,
        level: null,
        depart: null,
        beginTime: null,
        endTime: null
      },
      query: {},
      // 日期范围
      dateRange: [],
      // 奖项类型字典
      typeOptions: [],
      // 奖项级别字典
      levelOptions: [],
      // 学生获奖表格数据
      studentList: [],
      // 班级表格数据
      classList: [],
      // 表单参数
      chart: {}
    };
  },
  created() {
    getChartByCode(this.chartCode).then(response => {
      if(!response.data){
        this.msgError("报表不存在");
      }else{
        this.chart = response.data;
      }
    });
    this.getDicts("glory_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("glory_level").then(response => {
      this.levelOptions = response.data;
    });
    selectClazz().then(response => {
      this.classList = response.rows;
    });
  },
  methods: {
    remoteStudent() {
      this.loading = true;
      selectStudent({classId:this.queryParams.classId}).then(response => {
        this.stuentList = response.rows;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.dateRange && this.dateRange[0]){
        this.queryParams["beginTime"] = this.dateRange[0];
      }
      if(this.dateRange && this.dateRange[1]){
        this.queryParams["endTime"] = this.dateRange[1];
      }
      this.query = this.queryParams;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams["beginTime"] = null;
      this.queryParams["endTime"] = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      var ps = this.queryParams;
      ps.chartId = this.chart.id;
      exportDataset({id:this.chart.dsId, params:ps}).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
